package com.loop.unclelend.ui.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPageRepayBinding
import com.loop.unclelend.pro.ProFragment
import com.loop.unclelend.ui.activity.Act_Main
import com.loop.unclelend.ui.activity.Act_Message
import com.loop.unclelend.ui.activity.Act_Pin_Payment_Status
import com.loop.unclelend.ui.adapter.Adapter_Repay_Order
import com.loop.unclelend.ui.presenter.Pre_Repay
import com.loop.unclelend.ui.view.IView_Repay
import com.loop.unclelend.utils.event.UncleEventData
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/9 16:48
 * @Annotation：还款页
 */
class Fm_Repay: ProFragment(),IView_Repay {
    lateinit var binding: UiPageRepayBinding
    private var presenter: Pre_Repay? = null
    private var orderAdapter: Adapter_Repay_Order? = null

    override fun getUncleRootView(inflater: LayoutInflater): View {
        binding = UiPageRepayBinding.inflate(inflater)
        return binding.root
    }

    override fun initUncleView() {
        setUncleTitleBarVisibility(View.VISIBLE)
        //拦截返回
        isInterceptBack(true)
        setUncleBackBtnRes(R.mipmap.icon_menu)
        setUncleRightBtnRes(R.mipmap.icon_message)

        initRefreshLayout()
        initOrderRecyclerView()
    }

    override fun initUncleListener() {
        rootViewBinding.imgBack.setOnClickListener {
            val act = getAct() as Act_Main
            act.showUncleMenuFragment("Repay Page")
        }
        rootViewBinding.imgKefu.setOnClickListener {
            onIntent(Act_Message::class.java)
        }
    }

    override fun initUncleData() {
        if (presenter == null){
            presenter = Pre_Repay(this)
        }
        showUncleLoading()
        presenter!!.requestRepayOrderListAction()
    }

    /* 初始化刷新 */
    private fun initRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            presenter!!.requestRepayOrderListAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 订单列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun initOrderRecyclerView(){
//        binding.recyclerView.setHasFixedSize(true)
//        binding.recyclerView.isNestedScrollingEnabled = false
//        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
//        val decoration = DensityUtils.dp2px(9f)
//        binding.recyclerView.addItemDecoration(GridSpacingItemDecoration(3,decoration,false))
        val layoutManager = LinearLayoutManager(getContext())
        binding.rvOrder.layoutManager = layoutManager
        orderAdapter = Adapter_Repay_Order(binding.rvOrder)
        binding.rvOrder.adapter = orderAdapter
        orderAdapter!!.setOnItemChildClickListener { _, childView, position ->
            val item = this.orderAdapter!!.getItem(position)
            if (childView.id == R.id.btn_repay){
                presenter!!.requestPayLinkAction(item.optString("orderId"))
            }
        }
    }

    override fun showEmptyView() {
        binding.rvOrder.visibility = View.GONE
        binding.layoutEmpty.visibility = View.VISIBLE
    }

    override fun showOrderList(orderList: ArrayList<JSONObject>) {
        binding.rvOrder.visibility = View.VISIBLE
        binding.layoutEmpty.visibility = View.GONE

        orderAdapter!!.data = orderList
    }

    /**
     * 还款链接
     */
    override fun showLoanPayLink(linkUrl: String) {
        val uri = Uri.parse(linkUrl)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        startActivity(intent)
    }

    /**
     * PIN流程，进入订单状态查询
     */
    override fun showPaymentStatusPage(orderId: String,thirdOrderId: String) {
        Act_Pin_Payment_Status.start(requireContext(),orderId,thirdOrderId)
    }

    override fun hideUncleLoading() {
        super.hideUncleLoading()
        binding.refreshLayout.finishRefresh()
    }

    override fun onUncleEvent(event: UncleEventData) {
        super.onUncleEvent(event)
        if (event.eventType == UncleEventData.TYPE.UNCLE_LOAN_REPAY_REFRESH && presenter != null){
            presenter!!.requestRepayOrderListAction()
        }
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_REPAYMENT_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_REPAYMENT_END)
    }
}